<?php

namespace App\Extend;

class Response
{
	public $code;
	public $msg;
	public $data;
	public $count;

	// 默认常量
	const DEFAULT_CODE_SUCCESS = 0;
	const DEFAULT_CODE_ERROR = 500;
	const DEFAULT_MSG_SUCCESS = '成功';
	const DEFAULT_MSG_ERROR = '失败';
	const DEFAULT_DATA = [];
	const DEFAULT_COUNT = 0;

	public static function setError($msg=self::DEFAULT_MSG_ERROR, $code=self::DEFAULT_CODE_ERROR, $data=self::DEFAULT_DATA) {
		return (new self)
			->setCode($code)
			->setMsg($msg)
			->setData($data)
			->getResponse();
	}

	public static function setSuccess($data=self::DEFAULT_DATA, $count=self::DEFAULT_COUNT, $code=self::DEFAULT_CODE_SUCCESS) {
		return (new self)
			->setCode($code)
			->setMsg(self::DEFAULT_MSG_SUCCESS)
			->setData($data)
			->setCount($count)
			->getResponse();
	}

	public function getResponse() {
		return [
			'code' => $this->code,
			'msg'  => $this->msg,
            'data' => $this->data,
            'count'=> $this->count,
        ];
	}

	public function setCount($count) {
		$this->count = $count;
		return $this;
	}

	public function setMsg($msg) {
		$this->msg = $msg;
		return $this;
	}

	public function setCode($code) {
		$this->code = $code;
		return $this;
	}

	public function setData($data) {
		$this->data = $data;
		return $this;
	}
}